# -*- coding: utf-8 -*-
"""
Created on Sat Sep 17 10:06:05 2022

@author: 123
"""


#切片，分开 特征x 和 目标y

x, y = new_df.iloc[:,1:], new_df.iloc[:,0]

#使用smote抽样 

from imblearn.under_sampling import RandomUnderSampler




# 建立模型
rus = RandomUnderSampler(random_state=0)
# 欠抽样处理
X_resampled, y_resampled = rus.fit_resample(x, y)
# 合并数据
under_df = pd.concat([X_resampled, y_resampled],axis=1)
import numpy as np
n_features = 16
gen_cov = np.eye(n_features)

x_train=np.array(under_df.iloc[:,:-1])
x_test=np.array(x)

x_np = np.dot(x_train, gen_cov)

from sklearn.covariance import EmpiricalCovariance, MinCovDet
from sklearn.covariance import EllipticEnvelope

cov = EllipticEnvelope(random_state=0).fit(x_train)
x_test_predict_np=cov.predict(x_test)

#an_array = np.where((the_array > 30)
#an_array = np.where(x_test_predict_np = -1)
#x_test_predict_np_out= x_test_predict_np.where(x=-1) 
#print (x_test_predict_np)
#print(np.where(arr==3))

np.size(x_test_predict_np)
# Out[67]: 252000

np.sum(x_test_predict_np)
# Out[68]: 199250

np.size(np.where(x_test_predict_np==-1))
# Out[69]: 26341

#将异常值检测结果转为dataframe
#x_test_predict_np_t=x_test_predict_np
x_test_predict_pd=pd.DataFrame(x_test_predict_np)

#列重命名
p_col=['is_outlier']
x_test_predict_pd.columns=p_col

#is_outlier 字段和new_df 合并 
# df3 = pd.concat([df, df2], join="inner", axis=1)

new_df_outlier=pd.concat([new_df, x_test_predict_pd], join="inner", axis=1)


grouped_is_outlier=new_df_outlier['is_outlier'].groupby(new_df_outlier['is_outlier'])
print(grouped_is_outlier.count())


#is_outlier
#-1     26341
# 1    225659


grouped_is_Risk_Flag=new_df_outlier['Risk_Flag'].groupby(new_df_outlier['Risk_Flag'])
print(grouped_is_Risk_Flag.count())


#Risk_Flag
#0    221004
#1     30996



#异常值检验画图



#筛选出正常值 

#
#new_df_risk1=new_df_outlier[(new_df_outlier['is_outlier'] =1 )&(new_df_outlier['Risk_Flag'] = 1)]


#df.query('A=="A" & B=="B"')


